//
//  MineViewController.m
//  EasyToTesco
//
//  Created by mc on 16/1/31.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "MineViewController.h"

#import "HeaderFileForEasyToTesco.h"
//#import "MineHeaderView.h"
#import "ButtonView.h"


//个人专区或者商家服务界面的头文件
#import "AverageUserViewController.h"
#import "ShopUserViewController.h"
//我的订单头文件
#import "MyOrderViewController.h"

//登陆界面
#import "LoginViewController.h"

#import "AFNetworking.h"

//关于我们界面
#import "AboutUsViewController.h"

#import "SDImageCache.h"

#import "ReadLoginData.h"

//我的亿乐账户头文件
#import "MineYLViewController.h"
//意见反馈界面
#import "MessageReceiveViewController.h"

//积分详情页面
#import "IntegralViewController.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define NORMWIDTH [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
#define NORMHEIGHT [[UIScreen mainScreen] bounds].size.height / 667
#define FRAME [[UIScreen mainScreen] bounds]

@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
{
    CGFloat clearDisk;
    //用户名
    UILabel *usernamelabel;
    
    //缓存大小
    CGFloat hcvalue;
    
}
@property (nonatomic, strong)NSMutableDictionary *loginDic;
@property (nonatomic,copy)NSString *username;
@property(nonatomic, strong)UITableView *MineTableview;
@property (nonatomic,assign)BOOL isRegister;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    NSLog(@"%@",self.loginDic);
    NSLog(@"%@",self.username);
    if (!self.username.length == 0 && self.isRegister) {
        [self savelogindataToLocal];
        hcvalue = [self checkTmpSize];
        
    } else {
//        self.username = [[ReadLoginData readLoginData][@"data"] objectForKey:@"username"];
    }
    [self addMineUI];
//    [self.MineTableview reloadData];
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.MineTableview reloadData];
    NSLog(@"%@",self.loginDic);
    NSLog(@"%@",self.username);
    if (!self.username.length == 0 && self.isRegister) {
        [self savelogindataToLocal];
    } else {
//        self.username = [[ReadLoginData readLoginData][@"data"] objectForKey:@"username"];
    }

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (!self.username.length == 0 && self.isRegister) {
        [self savelogindataToLocal];
    }
}

- (void)addMineUI {
    
    [self addMineTableview];
    [self navi];
    [self addCancleButton];
    
}

/**
 *  注销界面
 */
- (void)addCancleButton
{
    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancle setImage:[UIImage imageNamed:@"v2_my_settings_icon"] forState:UIControlStateNormal];
    //    [cancle setTitle:@"注销" forState:UIControlStateNormal];
    [cancle setTintColor:[UIColor whiteColor]];
    [cancle setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    cancle.titleLabel.font = [UIFont systemFontOfSize:15];
    cancle.frame = CGRectMake(10, (self.secondView.frame.size.height - 10)/2 , 50, 50);
    [self.view addSubview:cancle];
    
    [cancle addTarget:self action:@selector(cancleButtonClick) forControlEvents:UIControlEventTouchUpInside];
}


- (void)cancleButtonClick
{
    //    NSLog(@"1");
    UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您将退出当前账号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    alview.tag = 210;
    [alview show];
    
}


- (void)navi{
    self.view.backgroundColor =[UIColor whiteColor];
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WEITH, 64)];
    top.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
    // top.backgroundColor = [UIColor redColor];
    [self.view addSubview:top];
    UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, WEITH, HIGHT * 0.1)];
    topLabel.text = @"我的";
    [top addSubview:topLabel];
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.textColor = [UIColor whiteColor];
    topLabel.font = [UIFont systemFontOfSize:18];
    
    
}

- (void)addMineTableview {
    //    self.MineTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.firstView.frame), WEITH, HIGHT-CGRectGetHeight(self.firstView.frame)-self.tabBarController.tabBar.frame.size.height) style:UITableViewStyleGrouped];
    self.MineTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 44) style:UITableViewStyleGrouped];
    self.MineTableview.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.MineTableview.dataSource = self;
    self.MineTableview.delegate = self;
    [self.view addSubview:self.MineTableview];
}



#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else if (section == 1) {
        return 2;
    }else if (section == 2){
        return 3;
    } else if (section == 3){
        return 3;
    } else {
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifierMineCell = @"Minecellidentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierMineCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifierMineCell];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"men-gray-icon.png"];
            cell.textLabel.text = @"我的亿乐账户";
        } else {
            cell.imageView.image = [UIImage imageNamed:@"wdjf-gray-icon.png"];
            cell.textLabel.text = @"我的积分";
            
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"wddd-gray-icon.png"];
            cell.textLabel.text = @"我的订单";
        } else if (indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"hdjl-gray-icon.png"];
            cell.textLabel.text = @"活动记录";
        }else {
#pragma mark -- 需要进行判断是商家还是普通会员,,,如果是商家的话就是商家服务,如果是普通会员的话就是个人专区
            cell.imageView.image = [UIImage imageNamed:@"sjfw-gray-icon.png"];
            NSDictionary *dic = [self readLoginData];
            if ((dic.allKeys.count == 0)||([[dic[@"data"] objectForKey:@"lb"] isEqualToString:@"0"])||([[dic[@"data"] objectForKey:@"lb"] isEqualToString:@"1"])) {
                cell.textLabel.text = @"个人专区";
            } else {
                cell.textLabel.text = @"商家服务";
            }
            
        }
    }else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"清除缓存";
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.3lfM", hcvalue];
        } else if (indexPath.row ==1) {
            cell.textLabel.text = @"关于我们";
        }else {
            cell.textLabel.text = @"意见反馈";
        }
    } else {
        cell.textLabel.text = @"退出账户";
    }
    
    //    cell.textLabel.text = [NSString stringWithFormat:@"%ld--%ld", indexPath.section, indexPath.row];
    return cell;
}

#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 0;
    } else {
        return 50*NORMHEIGHT;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 100*NORMHEIGHT;
    } else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 80*NORMHEIGHT;
    } else {
        return 1;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *headerview = [[UIView alloc] init];
        
        usernamelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50*NORMHEIGHT, WEITH, 50*NORMHEIGHT)];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[self readLoginData]];
        
        
//        NSLog(@"%@", dic);
        
        if (dic.allKeys.count==0) {
            usernamelabel.text = @"尚未登录,请登录";
        } else {
            usernamelabel.text = [NSString stringWithFormat:@"用户:%@", [dic[@"data"] objectForKey:@"username"]];
            
        }
        
        usernamelabel.textColor = [UIColor whiteColor];
        headerview.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
        usernamelabel.textAlignment = NSTextAlignmentCenter;
        usernamelabel.font = [UIFont systemFontOfSize:14];
        [headerview addSubview:usernamelabel];
        
        UIImageView *userImage = [[UIImageView alloc] initWithFrame:CGRectMake(WEITH / 2 - 25*NORMWIDTH, 10*NORMHEIGHT, 50*NORMWIDTH, 50*NORMHEIGHT)];
        userImage.layer.cornerRadius = 10;
        userImage.layer.masksToBounds = YES;
        //        userImage.center = headerview.center;
        [headerview addSubview:userImage];
        userImage.image = [UIImage imageNamed:@"icon_login"];
        
        
        UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        loginBtn.frame = usernamelabel.frame;
        
        [loginBtn addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [headerview addSubview:loginBtn];
        
        return headerview;
    } else {
        return nil;
    }
}

//跳转到登陆页面IntegralViewController
- (void)loginButtonClick{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[self readLoginData]];
    if (dic.allKeys.count==0) {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self presentViewController:loginVC animated:YES completion:nil];
    } else {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您已经登录,是否注销当前账户" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        alview.tag = 123;
        [alview show];
    }
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
        [self returnfootview:view];
    }
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            MineYLViewController *mineYlVC = [[MineYLViewController alloc] init];
            [self presentViewController:mineYlVC animated:YES completion:nil];
        } else {
            IntegralViewController *integalVC = [[IntegralViewController alloc] init];
            [self presentViewController:integalVC animated:YES completion:nil];
        }
    }
    
    if (indexPath.section == 2 && indexPath.row == 2) {
        
        NSDictionary *dic = [self readLoginData];
        if ((dic.allKeys.count == 0)||([[dic[@"data"] objectForKey:@"lb"] isEqualToString:@"0"])||([[dic[@"data"] objectForKey:@"lb"] isEqualToString:@"1"])) {
            AverageUserViewController *userVC =[[AverageUserViewController alloc] init];
            [self presentViewController:userVC animated:YES completion:nil];
            
            
        } else {
            
            ShopUserViewController *shopuserVC = [[ShopUserViewController alloc] init];
            [self presentViewController:shopuserVC animated:YES completion:nil];
        }
    }
    if (indexPath.section == 2&&indexPath.row == 0) {
        MyOrderViewController *myorderVC = [[MyOrderViewController alloc] init];
        [self presentViewController:myorderVC animated:YES completion:nil];
    }
    
    if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            //调用清除缓存的方法
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"清理缓存提示" message:[NSString stringWithFormat:@"共有缓存%lfM, 是否清理", [self checkTmpSize]]  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alertView.tag = 209;
            [alertView show];
            
        } else if (indexPath.row == 1) {
            AboutUsViewController *AboutUSVC = [[AboutUsViewController alloc] init];
            [self presentViewController:AboutUSVC animated:YES completion:nil];
        } else {
            MessageReceiveViewController *messageVc =[[MessageReceiveViewController alloc] init];
            [self presentViewController:messageVc animated:YES completion:nil];
        }
    }
    if (indexPath.section == 4) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您将退出当前账号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        alview.tag = 210;
        [alview show];
        
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 209) {
        if (buttonIndex == 1) {
            [self clearCache];
        }
        hcvalue = [self checkTmpSize];
        [self.MineTableview reloadData];
    }else if (alertView.tag == 210){
        if (buttonIndex == 0) {
#warning 注册判断
            self.isRegister = NO;
            [self cleanloginData];
        } else {
            self.isRegister = YES;
            return;
        }
        [self.MineTableview reloadData];
    } else if (alertView.tag == 123){
        if (buttonIndex == 0) {
            [self cleanloginData];
        } else {
            return;
        }
        [self.MineTableview reloadData];
    }
}

- (void)returnfootview:(UIView *)tempView {
    [self savelogindataToLocal];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[self readLoginData]];
    self.username = [dic[@"data"] objectForKey:@"username"];
    NSInteger cout = 0;
    if ((dic.allKeys.count == 0)||([[dic[@"data"] objectForKey:@"lb"] isEqualToString:@"0"])||([[dic[@"data"] objectForKey:@"lb"] isEqualToString:@"1"])) {
        cout = 3;
    } else {
        cout = 4;
    }
    
    
    CGFloat temp = WEITH/cout;
    
    
    ButtonView *countBtn = [[ButtonView alloc] initWithFrame:CGRectMake(10, 0, temp-20, 80*NORMHEIGHT)];
    countBtn.picImage.hidden = YES;//image = [UIImage imageNamed:@"czzh-color-icon.png"];
    countBtn.titleLabel.text = @"充值账户";
    countBtn.layer.borderWidth = 0;
    UILabel *valueLabel0 = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(countBtn.picImage.frame)-20, temp-20, 20)];
    valueLabel0.textColor = [UIColor redColor];
    valueLabel0.textAlignment = NSTextAlignmentCenter;
    valueLabel0.font = [UIFont systemFontOfSize:14];
    [countBtn addSubview:valueLabel0];
    [tempView addSubview:countBtn];
    
    
    ButtonView *Presentintegral = [[ButtonView alloc] initWithFrame:CGRectMake(temp+10, 0, temp-20, 80*NORMHEIGHT)];
    Presentintegral.picImage.hidden = YES;//image = [UIImage imageNamed:@"zsjf-color-icon.png"];
    Presentintegral.titleLabel.text = @"积分余额";
    Presentintegral.layer.borderWidth = 0;
    UILabel *valueLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(Presentintegral.picImage.frame)-20, temp-20, 20)];
    valueLabel1.textColor = [UIColor redColor];
    valueLabel1.textAlignment = NSTextAlignmentCenter;
    valueLabel1.font = [UIFont systemFontOfSize:14];
    
    
    
    [Presentintegral addSubview:valueLabel1];
    [tempView addSubview:Presentintegral];
    
    ButtonView *Consumptionamount = [[ButtonView alloc] initWithFrame:CGRectMake(temp*2+10, 0, temp-20, 80*NORMHEIGHT)];
    Consumptionamount.picImage.hidden = YES;
    Consumptionamount.layer.borderWidth = 0;
    Consumptionamount.titleLabel.text = @"总消费额";
    UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(Consumptionamount.picImage.frame)-20, temp-20, 20)];
    valueLabel.textColor = [UIColor redColor];
    valueLabel.textAlignment = NSTextAlignmentCenter;
    valueLabel.font = [UIFont systemFontOfSize:14];
    
    if (dic.allKeys.count==0) {
        valueLabel1.text = @"暂无";
        valueLabel0.text = @"暂无";
        valueLabel.text = @"暂无";
    } else {
        if([self.loginDic objectForKey:@"yue"] == nil || [self.loginDic objectForKey:@"zhongjifen"] == nil || [self.loginDic objectForKey:@"jifenyue"] == nil) {
            
            valueLabel0.text = [NSString stringWithFormat:@"%.1f", [[dic[@"data"] objectForKey:@"yue"] floatValue]];
            valueLabel1.text = [NSString stringWithFormat:@"%.1f", [[dic[@"data"] objectForKey:@"zjf"] floatValue]];
            valueLabel.text = [NSString stringWithFormat:@"%.1f", [[dic[@"data"] objectForKey:@"xfe"] floatValue]];
        } else {
            NSString *string= [self.loginDic objectForKey:@"yue"];
            valueLabel0.text = [NSString stringWithFormat:@"%.1f", string.floatValue];
            
            NSString *string1 = [self.loginDic objectForKey:@"zongjifen"];
            valueLabel1.text = [NSString stringWithFormat:@"%.1f", string1.floatValue];
            
            NSString *string2 = [self.loginDic objectForKey:@"jifenyue"];
            valueLabel.text = [NSString stringWithFormat:@"%.1f", string2.floatValue];
        }
    }
    
    [Consumptionamount addSubview:valueLabel];
    [tempView addSubview:Consumptionamount];
    if (cout == 4) {
        ButtonView *userCountMoney = [[ButtonView alloc] initWithFrame:CGRectMake(temp*3, 0, temp, 80*NORMHEIGHT)];
        userCountMoney.picImage.hidden = YES;
        userCountMoney.layer.borderWidth = 0;
        userCountMoney.titleLabel.text = @"营业账户余额";
        UILabel *valueLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(userCountMoney.picImage.frame)-20, temp, 20)];
        valueLabel2.textColor = [UIColor redColor];
        valueLabel2.textAlignment = NSTextAlignmentCenter;
        valueLabel2.font = [UIFont systemFontOfSize:14];
        //        valueLabel.backgroundColor = [UIColor redColor];
        [userCountMoney addSubview:valueLabel2];
        [tempView addSubview:userCountMoney];
        if (dic.allKeys.count==0) {
            valueLabel2.text = @"暂无";
        } else {
            valueLabel2.text = [NSString stringWithFormat:@"%.1f", [[dic[@"data"] objectForKey:@"yyye"] floatValue]];
        }
    }
    
    for (NSInteger i=0; i<cout; i++) {
        UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        clearBtn.frame = CGRectMake(temp*i, 0, temp, 80*NORMHEIGHT);
        clearBtn.tag = 350+i;
        //        [clearBtn setBackgroundColor:[UIColor redColor]];
        [clearBtn addTarget:self action:@selector(clearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [tempView addSubview:clearBtn];
    }
    
    for (NSInteger j=0; j<(cout-1); j++) {
        UIView *linview = [[UIView alloc] initWithFrame:CGRectMake(temp+temp*j, 2, 1, 78*NORMHEIGHT)];
        linview.layer.borderWidth = 1;
        linview.layer.borderColor = [UIColor colorWithRed:127/255. green:127/255. blue:127/255. alpha:1].CGColor;
        [tempView addSubview:linview];
    }
    
}

- (void)clearBtnClick:(UIButton *)button {
    
    if (button.tag == 351) {
        IntegralViewController *integVC = [[IntegralViewController alloc] init];
        [self presentViewController:integVC animated:YES completion:nil];
    }
}


#pragma 清理缓存图片

//清除cache文件
-  (void)clearCache {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        
        //清除sdwebImage缓存的图片
        [[SDImageCache sharedImageCache] getSize];
        [[SDImageCache sharedImageCache] clearDisk];
        
        [[SDImageCache sharedImageCache] clearMemory];//可有可无
        
        //        NSLog(@"clear disk");
        
        float tmpSize = [self checkTmpSize];
        
        clearDisk = tmpSize*1024;
        NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize * 1024];
        
//        NSLog(@"%@", clearCacheName);
        //清除cache文件
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
        
        //        NSLog(@"files :%lu",[files count]);
        
        for (NSString *p in files) {
            
            NSError *error;
            
            NSString *path = [cachPath stringByAppendingPathComponent:p];
            
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                
                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                
            }
            
        }
        
        [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
    
}

//计算缓存大小
-(float)checkTmpSize{
    float totalSize = 0;
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:cachPath];
    for (NSString *fileName in fileEnumerator)
    {
        NSString *filePath = [cachPath stringByAppendingPathComponent:fileName];
        
        NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        
        unsigned long long length = [attrs fileSize];
        
        totalSize += length / 1024.0 / 1024.0;
    }
    //    NSLog(@"tmp size is %.2f",totalSize);
    
    return totalSize;
}


//清除缓存
- (void)clearCacheSuccess {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缓存清理成功"  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}

//取出存储的登录信息数据
- (NSMutableDictionary *)readLoginData {
    self.isRegister = YES;
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    
//    NSLog(@"---%@", path);
    //获取一个plist文件
    NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
    //读文件
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filename];//[NSMutableArray arrayWithContentsOfFile:filename];
    return dic;
}


//清除历史记录数据
- (void)cleanloginData {
    self.isRegister = NO;
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
//    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dic writeToFile:filename atomically:YES];
    
}

- (void)savelogindataToLocal{
    
    NSDictionary *dic = [ReadLoginData readLoginData];
    NSString *username = [dic[@"data"] objectForKey:@"username"];
    
    if (!username.length == 0 && self.isRegister) {
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        self.username = username;
        [manager POST:@"http://www.ylgw8.com/AppUser/UserInfo" parameters:@{@"username":username} success:^(NSURLSessionDataTask *task, id responseObject) {
            
            NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
            
            
            
            self.loginDic = dict[@"data"];
            NSLog(@"%@",self.loginDic);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
        //1. 创建一个plist文件
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *path=[paths objectAtIndex:0];
        //    NSLog(@"path = %@",path);
        NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
        
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filename];
        
        //    NSLog(@"%@",[dict[@"data"] objectForKey:@"username"]);
        
        if (self.loginDic.count != 0) {
            [dict[@"data"] setValue:[self.loginDic objectForKey:@"yyye"] forKey:@"yyye"];
            [dict[@"data"] setValue:[self.loginDic objectForKey:@"zongjifen"] forKey:@"zjf"];
            [dict[@"data"] setValue:[self.loginDic objectForKey:@"jifenyue"] forKey:@"xfe"];
            [dict[@"data"] setValue:[self.loginDic objectForKey:@"yue"] forKey:@"yue"];
        }
        
            NSLog(@"%@",dict);
        [dict writeToFile:filename atomically:YES];
        
        //    [self.MineTableview reloadData];

    }
    
}

/*
 lb = 4;
 phone = 18000000000;
 sessionid = 1209819;
 username = 18000000000;
 xfe = 57467;
 youka = 1111111111111111122;
 yue = "17321.84";
 yyye = "30219.36";
 zhanghao = 111111111111111;
 zjf = "1001.5";
 */

- (void)loadUserInfo {
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}




@end
